import time
from time import sleep

from selenium.webdriver.common.by import By

from base.base import Base

url = 'http://192.168.8.222/admin.php'


class WarehousePage(Base):
    def login(self):
        self.save_cookie('../data/w_warehouse_cookies.json')
        sleep(20)

    def login_admin(self):
        self.set_cookie('../data/w_warehouse_cookies.json')
        self.driver.get(url)
        self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[10]/a'))
        # 仓库管理
        self.click((By.XPATH, '//*[@id="power-menu-438"]/li[1]/a'))
        self.go_iframe((By.ID, "ifcontent"))

    def add_warehouse(self, name, nickname, weight, contact, phone, address):
        # sleep(20)
        # 新增
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/a[1]'))
        # 名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/input'), name)
        # 别名
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[2]/input'), nickname)
        # 权重
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/input'), weight)
        # 联系人
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), contact)
        # 联系电话
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[5]/input'), phone)
        # 选择地区
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[1]/a/span'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[1]/div/ul/li[3]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[2]/a/span'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[2]/div/ul/li[3]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[3]/a/span'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[3]/div/ul/li[2]'))
        self.click((By.XPATH, '//*[@id="map-location-submit"]'))
        # 详细地址
        self.input((By.XPATH, '//*[@id="form-address"]'), address)
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[8]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[9]/div/div/span[3]'))
        # 保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[10]/button'))
        sleep(1)
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def delete_warehouse(self):
        # self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[10]/a'))
        # self.click((By.XPATH, '//*[@id="power-menu-438"]/li[1]/a'))
        # self.go_iframe((By.ID, "ifcontent"))
        # 找到复选框并选中
        self.click((By.CSS_SELECTOR, 'td:nth-child(1) > div > label'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[1]'))
        self.click((By.CSS_SELECTOR, 'div.am-modal-footer > span:nth-child(2)'))
        sleep(1)
        # 返回获取的文本
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))
        # print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))

    def reset_search_warehouse(self, name, min, max, nickname, phone, address):
        # self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[10]/a'))
        # self.click((By.XPATH, '//*[@id="power-menu-438"]/li[1]/a'))
        # self.go_iframe((By.ID, "ifcontent"))
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[2]/input'), name)
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[3]/div/input[1]'), min)
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[3]/div/input[2]'), max)
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[4]/div/ul/li/input'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[4]/div/div/ul/li[2]'))
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[5]/input'), nickname)
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[6]/input'), phone)
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[7]/div/ul/li/input'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[7]/div/div/ul/li[1]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[8]/div/ul/li/input'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[8]/div/div/ul/li[1]'))
        # 定位到需要水平滚动的元素
        element = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[1]/th[11]')
        self.driver.execute_script("arguments[0].scrollIntoView();", element)
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[9]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[9]/div/div/ul/li[1]'))
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[10]/input'), address)
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[2]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/a[2]'))

    def sidebar_warehouse(self, name, nickname, weight, contact, phone, address):
        # 点击仓库管理
        # self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[10]/a'))
        # 进入仓库管理
        # self.click((By.XPATH, '//*[@id="power-menu-438"]/li[1]/a'))
        # 进入iframe页面
        # self.go_iframe((By.ID, "ifcontent"))
        # 点击详情
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-default.am-btn-xs.am-radius.am-btn-block.submit-popup'))
        sleep(1)
        # 退出详情
        self.click((By.XPATH, '//div[starts-with(@id, "am-modal-")]/div/span'))
        # 点击编辑
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > a'))
        # 进入iframe页面
        # self.go_iframe((By.ID, "ifcontent"))
        # 编辑名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/input'), name)
        # 编辑别名
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[2]/input'), nickname)
        # 编辑权重
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[3]/input'), weight)
        # 编辑联系人
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), contact)
        # 编辑联系人电话
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[5]/input'), phone)
        # 选择省市县
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[1]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[1]/div/ul/li[25]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[2]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[2]/div/ul/li[2]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[3]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/div[3]/div/ul/li[11]'))
        # 选择详细地址
        self.click((By.XPATH, '//*[@id="map-location-submit"]'))
        self.input((By.XPATH, '//*[@id="form-address"]'), address)
        # 保存并删除
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[10]/button'))
        sleep(1)
        self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))
        # print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-danger.am-btn-xs.am-radius.am-btn-block.submit-delete'))
        self.click((By.CSS_SELECTOR, 'div.am-modal-footer > span:nth-child(2)'))
        sleep(1)
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))
        # print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))

    # def warehouse_manage(self, name, nickname, weight, contact, phone, address, min, max):
    #     self.login_admin()
    #     self.add_warehouse(name, nickname, weight, contact, phone, address)
    #     self.delete_warehouse()
    #     self.reset_search_warehouse(name, min, max, nickname, phone, address)
    #     self.sidebar_warehouse(name, nickname, weight, contact, phone, address)


if __name__ == '__main__':
    wh = WarehousePage(url)
    wh.login_admin()
    # wh.login()
    wh.add_warehouse("zs", 'ls', '55', 'le', '12345678901', '河东区委食堂')
    wh.delete_warehouse()
    wh.reset_search_warehouse('zs', '10', '60', 'le', '12345678901', '河东区委食堂')
    wh.sidebar_warehouse('12', '34', '56', '78', '98765432100', '陈亮村')
